<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Class Applyads
 * @author  heyu
 */
class Applyads extends MY_Controller {
    public $mAds;

	public function __construct(){
		parent::__construct();
        $this->loadModel('MerchantAdsModel', 'mAds', 'merchant');
	}

	/**
	 * 首页
	 */
	public function index(){
        $position = getCustomConfig('merchant_position');
        $data = array(
            'position' => $position,
            'title' => '广告申请记录'
        );
		$this->view('config/applyAds', $data, 'datatable,datepicker,validation');
	}

    /**
     * 获取列表
     */
    public function getList(){
        $position = getCustomConfig('ads_position');
        $_GET['iSortCol_0'] = get('iSortCol_0')-1;
        $data = $this->mAds->adminPcGetList(0, $this->countyItem);
        foreach ($data['data'] as $k => $v){
            $data['aaData'][$k][] = $v['shop_name'];
            $data['aaData'][$k][] = $position[$v['ad_position']];
            $data['aaData'][$k][] = $v['day'];
            $data['aaData'][$k][] = dateFormat($v['addtime']);
            if ($v['status'] == 4){
                $data['aaData'][$k][] = '<a href="#" class="popovers" data-placement="left" data-trigger="hover" data-content="'.
                    $v['note'].'" data-original-title="拒绝原因">'.$this->mAds->getStatus($v['status'], true).'</a>';
            }else{
                if ($v['endtime'] > 0 && $v['endtime'] < strtotime(date('Y-m-d',time()))){
                    $status = 3;
                }else{
                    $status = $v['status'];
                }
                $data['aaData'][$k][] = $this->mAds->getStatus($status, true);
            }
            if ($v['status'] == 1) {
                $btnConfig = adminCreateBtn(array(
                    'label' => ' 上传广告',
                    'attr' => array(
                        'href' => getUrl('config/ads/add') . '?related_id='.$v['id'].'&merchant=' . $v['merchant_id'],
                        'class' => 'btn btn-xs blue-stripe default',
                    ),
                    'tag' => 'a'
                ));

                $btnConfig .= ' '.adminCreateBtn(array(
                        'label' => ' 拒绝',
                        'attr' => array(
                            'href' => 'javascript:;',
                            'class' => 'btn btn-xs red-stripe default refuse',
                            'data-id' => $v['id'],
                            'data-nickname' => $v['shop_name'],
                        ),
                        'tag' => 'a'
                    ));
                $data['aaData'][$k][] = $btnConfig;
            }else{
                $data['aaData'][$k][] = '';
            }
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 拒绝
     */
    public function refuse(){
        //表单验证
        $vConf = array(
            array('id', 'id', 'required|is_natural_no_zero'),
            array('note', '拒绝原因', 'required'),
        );
        $this->formDataValidation($vConf);
        $id = (int)post('id');
        $note = post('note');
        $info = $this->mAds->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '数据不存在');
        }
        if ($info['status'] != 1){
            $this->ajaxReturn(0, '不能拒绝');
        }
        $tA = $this->mAds->save(array('status' => 4, 'note' => $note), $id);
        if (!$tA){
            $this->ajaxReturn(0, '拒绝失败');
        }
        $this->ajaxReturn(1, '拒绝成功');
    }
}
